//
//  MyKaQuanBaseViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/17.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MyKaQuanBaseViewController.h"
//view

#import "MyKaQuanCell.h"

//controller
#import "CouponDetaiController.h"

//model
#import "MyCouponModel.h"
#import "MyWeiYongCouponModel.h"

//保持水平
#define kNodataFrame  CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-100-40)

@interface MyKaQuanBaseViewController ()<UITableViewDelegate,UITableViewDataSource>


@end

@implementation MyKaQuanBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    
    [self setupRefresh];
}

- (void)setupView
{
    self.tableView.frame = self.view.bounds;
//    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 40+FD_TabbarSafeBottomMargin, 0); //设置上padding 40 (titleView的高)
    [self.view addSubview:self.tableView];
    
    self.tableView.backgroundColor = [UIColor whiteColor];//找不到灰色背景是哪一个,直接覆盖。
    self.tableView.tableFooterView = [UIView new];
    
    //1-3添加无数据占位图
    [self.tableView addSubview:self.nodataView];
    self.nodataView.hidden = YES; //默认隐藏
}


- (void)setupRefresh {
    FDRefreshHader *refreshHeader = [FDRefreshHader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_header = refreshHeader;
}





////!!!!:getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[MyKaQuanCell class] forCellReuseIdentifier:@"MyKaQuanCell"];
        
        //取消线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (FDNowDataView *)nodataView {
    if (!_nodataView) {
        _nodataView = [[FDNowDataView alloc] initWithFrame:kNodataFrame];
    }
    return _nodataView;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyKaQuanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyKaQuanCell"];
    MyWeiYongCouponModel *weiyongModel = self.dataArr[indexPath.row];
    cell.model = weiyongModel.coupon;
    
    //cell上按钮回调
    cell.myKaQuanCellClickWaiSongDianCanBlock = ^{
        if (self.mykaquanBaseVCClickCellBtnBlock) {
            self.mykaquanBaseVCClickCellBtnBlock();
        }
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}


#pragma mark - 请求数据
- (void)loadData {
    FDLog(@"%s 子类需要重写",__FUNCTION__ );
}

/**请求可用的优惠券列表*/
//- (void)loadUsableCouponData {
//    [FDHttpAPi requestSYUserCouponUsableSuccessBlock:^(id responseObjc) {
//        
//        NSString *event = responseObjc[@"event"];
//        if ([event isEqualToString:@"SUCCESS"]) { //请求成功
//            NSArray *weiYongArr = responseObjc[@"data"];
//            
//            //解析数据
//            NSMutableArray *weiyongArrM = [NSMutableArray array];
//            
//            for (NSDictionary *dic1 in weiYongArr) {
//                NSDictionary *couponDic = dic1[@"coupon"];
//                //先将coupon转换为model
//                MyCouponModel *couponModel = [MyCouponModel mj_objectWithKeyValues:couponDic];
//                
//                //重新创建一个可变字典
//                NSMutableDictionary *newDic = [NSMutableDictionary dictionary];
//                [newDic setValue:couponModel forKey:@"coupon"];
//                [newDic setValue:dic1[@"id"] forKey:@"id"];
//                
//                //字典转模型
//                MyWeiYongCouponModel *weiYongModel = [MyWeiYongCouponModel mj_objectWithKeyValues:newDic];
//                //转入数组
//                [weiyongArrM addObject:weiYongModel];
//            }
//            
//            //回到主线程中刷新数据
//            dispatch_async(dispatch_get_main_queue(), ^{
//                self.dataArr = weiyongArrM;
//                NSString *str1 = [NSString stringWithFormat:@"未使用(%ld)",weiyongArrM.count];
//                NSString *str2 = @"已使用/失效";
//                NSArray *titleArr = @[str1,str2];
//                
//                if (self.dataArr.count) {
//                    self.nodataView.hidden = YES;
//                }else {
//                    self.nodataView.hidden = NO;
//                }
//                [self.tableView reloadData];
//            });
//        }
//    } failBlock:^(NSError *error) {
//        
//    }];
//}

@end
